package com.task;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestPy {
    public static void main(String args[]) {

        // PythonInterpreter interpreter = new PythonInterpreter();
        // interpreter.execfile("E:\\python\\python_demo\\inputpy.py");
        // interpreter.execfile("E:\\python\\python_demo\\see_xiaohua.py");
        try {

            execCmd("python  D:\\data\\guoji.py");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String execCmd(String cmd) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        // 执行命令
        Process process = runtime.exec(cmd);
        // 接收命令执行的输出信息
        BufferedReader infoReader = new BufferedReader(new InputStreamReader(
                process.getInputStream()));
        String info = "";
        StringBuilder sb = new StringBuilder();
        while ((info = infoReader.readLine()) != null) {
            sb.append(info);
        }
        // 接收命令执行的错误信息
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(
                process.getErrorStream()));
        String error = "";
        while ((error = errorReader.readLine()) != null) {
            sb.append(error);
        }
        return sb.toString();
    }
}
